package com.thirtydays.frame.util

import android.content.Context
import android.net.Uri
import android.text.TextUtils
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.engine.CompressEngine
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnCallbackListener
import com.luck.picture.lib.utils.DateUtils
import com.luck.picture.lib.utils.SdkVersionUtils
import com.thirtydays.frame.base.BaseApp
import com.thirtydays.frame.util.ext.isNull
import top.zibin.luban.Luban
import top.zibin.luban.OnCompressListener
import java.io.File


/**
 * @创建者：小垚
 * @时间：2022/2/25  18:02
 * @描述：
 */
class ImageCompressEngine : CompressEngine {
    override fun onStartCompress(
        context: Context,
        list: ArrayList<LocalMedia>,
        listener: OnCallbackListener<ArrayList<LocalMedia>>
    ) {
        if (list.isNull() || context.isNull() || listener.isNull()) {
            return
        }

        val compress = mutableListOf<Uri>()
        list.forEachIndexed { _, localMedia ->
            val availablePath = localMedia.availablePath
            val uri: Uri =
                    if (PictureMimeType.isContent(availablePath) || PictureMimeType.isHasHttp(availablePath)) Uri.parse(
                        availablePath
                    ) else Uri.fromFile(File(availablePath))
            compress.add(uri)
        }
        if (compress.size == 0) {
            listener.onCall(list)
            return
        }
        Luban.with(BaseApp.appContent)
            .load(compress)
            .ignoreBy(100)
            .filter { path ->
                PictureMimeType.isUrlHasImage(path) && !PictureMimeType.isHasHttp(
                    path
                )
            }
            .setRenameListener { filePath ->
                val indexOf = filePath.lastIndexOf(".")
                val postfix = if (indexOf != -1) filePath.substring(indexOf) else ".jpg"
                DateUtils.getCreateFileName("CMP_").toString() + postfix
            }
            .setCompressListener(object : OnCompressListener {
                override fun onStart() {}
                override fun onSuccess(index: Int, compressFile: File) {
                    val media = list[index]
                    if (compressFile.exists() && !TextUtils.isEmpty(compressFile.absolutePath)) {
                        media.isCompressed = true
                        media.compressPath = compressFile.absolutePath
                        media.sandboxPath = if (SdkVersionUtils.isQ()) media.compressPath else null
                    }
                    if (index == list.size - 1) {
                        listener.onCall(list)
                    }
                }

                override fun onError(index: Int, e: Throwable) {
                    if (index != -1) {
                        val media = list[index]
                        media.isCompressed = false
                        media.compressPath = null
                        media.sandboxPath = null
                        if (index == list.size - 1) {
                            listener!!.onCall(list)
                        }
                    }
                }
            }).launch()
    }
}